package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.MmreT;
import cn.e4j.modular.crm.mapper.MmreTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 服务区域维护 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-11
 */
@Service
public class Gwai640ServiceImpl extends ServiceImpl<MmreTMapper, MmreT>{

    @Transactional(readOnly = true)
    public List<Dto> getListData(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getListData(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getOrgsList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getOrgsList(page, dto);
        return listData;
    }

    @Transactional
    public void addData(MmreT mmreT){
        Dto dto = Dtos.newDto();
        dto.put("ENT",mmreT.getMMREENT());
        dto.put("MMRE001",mmreT.getMMRE001());
        //mmreT.setMMRE002(this.baseMapper.getMmreSeq(dto));
        mmreT.setMMRE002(InvokeUtil.invokeBeanMethod("mmreTMapper,getMmreSeq",dto));
        mmreT.insert();
    }

    @Transactional(readOnly = true)
    public Boolean existedTheParams(MmreT mmreT) {
        QueryWrapper<MmreT> mmreTQueryWrapper = new QueryWrapper<>();
        mmreTQueryWrapper.eq("MMREENT",mmreT.getMMREENT());
        mmreTQueryWrapper.eq("MMRE001",mmreT.getMMRE001());
        mmreTQueryWrapper.eq("MMRE003",mmreT.getMMRE003());
        mmreTQueryWrapper.eq("MMRE004",mmreT.getMMRE004());
        return this.baseMapper.selectList(mmreTQueryWrapper).size() > 0;
    }

    @Transactional
    public void editData(MmreT mmreT){
        //修改1
        UpdateWrapper<MmreT> mmreTUpdateWrapper = new UpdateWrapper<>();
        mmreTUpdateWrapper.eq("MMREENT",mmreT.getMMREENT());
        mmreTUpdateWrapper.eq("MMRE001",mmreT.getMMRE001());
        mmreTUpdateWrapper.eq("MMRE002",mmreT.getMMRE002());

        mmreT.update(mmreTUpdateWrapper);
    }

    @Transactional
    public void deleteData(MmreT mmreT){
        delete(mmreT);
    }

    @Transactional
    public void deleteDataBatch(List<MmreT> mmreTList){
        for(int i=0; i < mmreTList.size(); i++){
            delete(mmreTList.get(i));
        }
    }

    private void delete(MmreT mmreT){
        //修改1
        UpdateWrapper<MmreT> mmreTUpdateWrapper = new UpdateWrapper<>();
        mmreTUpdateWrapper.eq("MMREENT",mmreT.getMMREENT());
        mmreTUpdateWrapper.eq("MMRE001",mmreT.getMMRE001());
        mmreTUpdateWrapper.eq("MMRE002",mmreT.getMMRE002());

        mmreT.delete(mmreTUpdateWrapper);
    }


}
